﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace osm2mongo
{
	class Program
	{
		static void Main(string[] args)
		{

			string inputFile = getCommandParameter("-i", args);
			string mongoHost = getCommandParameter("-h", args);
			string mongoDb = getCommandParameter("-d", args);
			
			// Create the importer object
			OsmImportManager importer = new OsmImportManager(inputFile, mongoHost, mongoDb);

			try
			{
				importer.ImportData();

				Console.WriteLine("Processing complete.");
			}
			catch (Exception ex)
			{
				Console.WriteLine("Error importing OSM data. Message: " + ex.Message);
			}
			if (System.Diagnostics.Debugger.IsAttached)
			{
				Console.ReadLine();
			}

		}

		/// <summary>
		/// Gets the value of the parameter from command line arguments
		/// </summary>
		/// <param name="paramName"></param>
		/// <param name="args"></param>
		/// <returns></returns>
		private static string getCommandParameter(string paramName, string[] args)
		{

			string paramValue = "";

			// Loop through the command line parameters
			for (int i = 0; i < args.Length; i++)
			{
				// return the next parameter index, which is the value of the parameter
				if (i + 1 >= args.Length)
				{
					break;
				}
				else if (args[i] == paramName)
				{
					paramValue = args[i + 1];
					break;
				}
			}

			// return the parameter value
			return paramValue;
		}

		private static bool commandParameterExists(string paramName, string[] args)
		{

			// Loop through the command parameters, if it exists, return true, otherwise return false.
			foreach (string arg in args)
			{
				if (arg == paramName)
				{
					return true;
				}
			}

			// Command parameter not found, return false
			return false;
		}
	}
}
